package global

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"time"
)

var (
	DB            *gorm.DB
	ROUTER        *gin.Engine
	TOKEN_SIGNING = []byte("secret_test")
	TOKEN_NAME    = "authorization"
)

func InitGlobal() {
	initRoute()
	initDB()
}
func initRoute() {
	r := gin.Default()
	ROUTER = r
}

func initDB() {
	dsn := "root:root@tcp(localhost:3306)/test_db?charset=utf8mb4&parseTime=True&loc=Local"
	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
		//Logger: logger.Default.LogMode(logger.Info),
	})
	if err != nil {
		fmt.Println(err)
		return
	}
	sqlDB, _ := db.DB()
	sqlDB.SetMaxOpenConns(100)
	sqlDB.SetMaxIdleConns(10)
	sqlDB.SetConnMaxLifetime(time.Hour)
	DB = db
}
